<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>場地預借推薦服務</title>
<script language="JavaScript">
  function formCheck()
  {
     if(document.form1.person.value == ""){
	      alert("請填寫可容納人數");
		  document.form1.person.focus()
		  return false;
	 }
	 if(isNaN(document.form1.person.value)){
	 	  alert("可容納人數需填寫數值");
		  document.form1.person.focus()
		  return false;
	 }
  }
</script>
<style type="text/css">
<!--
.style3 {
	font-size: 12px;
	font-weight: bold;
	color: #FF0000;
}
-->
</style>
</head>
<body>
<% 
if(session.getAttribute("access") == "y" ){%>
	<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
	<% 
	String sql="select * from PLACE_CLASS";
	ResultSet rs=mydb.executeQuery(sql); 
	%>
	<table width="750" border="0" align="center">
  		<tr>
    		<td width="30" rowspan="4"><jsp:include page="../frameset/left.jsp" flush="true" ></jsp:include></td>
    		<td colspan="2"><jsp:include page="../frameset/top.jsp" flush="true" ></jsp:include></td>
    		<td width="40" rowspan="4"><jsp:include page="../frameset/right.jsp" flush="true" ></jsp:include></td>
  		</tr>
  		<tr>
    		<td rowspan="2"><jsp:include page="../frameset/mean.jsp" flush="true" ></jsp:include></td>
    		<td height="32"><jsp:include page="../frameset/book_mean.jsp" flush="true" ></jsp:include></td>
  		</tr>
  		<tr>
    		<td>
    		<%--內頁--%>
    		<%
    		String date=""; %>
    		<table width="550" border="0">
  				<tr>
    				<td width="54%"><jsp:include page="showCal2.jsp" flush="true" ></jsp:include></td>
    				<td width="18%">預借日期</td>
					<td width="28%">
					<%
    				if(request.getParameter("mon")==null){%>
    					<span class="style3">*請點選旁邊的行事曆</span>
    				<%
    				}
     				else{
     					date=request.getParameter("year")+"/"+request.getParameter("mon")+"/"+request.getParameter("day");
    				%>
    					<input name="time" type="text" id="datee" value="<%=date%>">
    				<%
     				}%>
					</td>
  				</tr>
  				<tr>
    				<td colspan="3"><br/></td>
  				</tr>
  				<%
    			if(request.getParameter("mon")!=null){
    			%>
  		 		<tr>
    				<td colspan="3">
    				<form id="form1" name="form1" method="post" action="bPSearch.jsp">
						<table width="400" border="1" align="center">	
  							<tr>
    							<td colspan="2" align="center" bgcolor="#FFFF66">場地資料調查</td>
  							</tr>
  							<tr>
    							<td>場地類別</td>
    							<td>
									<select name="class">
									<option value="0">不拘</option>
    								<% while(rs.next()){%>
    								<option value="<%=rs.getString("PC_ID")%>" ><%=rs.getString("PC_NAME")%></option>
    								<%}
    								%>
  									</select>
    							</td>
  							</tr>
  							<tr>
    							<td>可容納人數</td>
    							<td>大於<input type="text" name="person" maxlength="10" size="5"/>人</td>
  							</tr>
  							<tr>
    							<td>是否有空調</td>
    							<td><input name="air" type="radio" value="無" checked="checked" />無
  								<input name="air" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr>
    							<td>隔音效果</td>
    							<td><input name="deafening" type="radio" value="差" checked="checked" />差
  								<input name="deafening" type="radio" value="普通" />普通
  								<input name="deafening" type="radio" value="好" />好
  								</td>
  							</tr>
   							<tr>
    							<td>音響設備</td>
    							<td><input name="acoustics" type="radio" value="無" checked="checked" />無
  								<input name="acoustics" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr>
    							<td>麥克風</td>
    							<td><input name="mir" type="radio" value="無" checked="checked" />無
  								<input name="mir" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr>
    							<td>投影屏幕</td>
    							<td><input name="screen" type="radio" value="無" checked="checked" />無
  								<input name="screen" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr>
    							<td>投影機</td>
    							<td><input name="projector" type="radio" value="無" checked="checked" />無
  								<input name="projector" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr>
    							<td>燈光效果</td>
    							<td><input name="light" type="radio" value="白燈" checked="checked" />白燈
  								<input name="light" type="radio" value="黃燈" />黃燈
  								<input name="light" type="radio" value="兩段式" />兩段式
  								</td>
  							</tr>
  							<tr>
    							<td>無線上網</td>
    							<td><input name="wireless" type="radio" value="無" checked="checked" />無
  								<input name="wireless" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr>
    							<td>LCD跑馬燈</td>
    							<td><input name="lcd" type="radio" value="無" checked="checked" />無
  								<input name="lcd" type="radio" value="有" />有
  								</td>
  							</tr>
  							<tr align="center">
    							<input type="hidden" name="b_date" id="b_date" value="<%=date%>">
    							<td colspan="2" ><input name="reset" type="reset" value="重新填寫" /><input name="searchPlace" type="submit" id="set" onClick="return formCheck()" value="確定" /></td>
  							</tr>
						</table>
    				</form>
    				</td>
  				</tr>
  				<%
    			}
    			else{%>
     				<tr>
     					<td colspan="2"></td>
    				</tr>
    			<%
    			}
    			if(request.getParameter("class")!=null){//使用者已輸入搜尋資料
    				String pair =new String(request.getParameter("air").getBytes("iso-8859-1"),"utf-8");
					String pdea=new String(request.getParameter("deafening").getBytes("iso-8859-1"),"utf-8");
					String pacou=new String(request.getParameter("acoustics").getBytes("iso-8859-1"),"utf-8");
					String mir=new String(request.getParameter("mir").getBytes("iso-8859-1"),"utf-8");
					String scr=new String(request.getParameter("screen").getBytes("iso-8859-1"),"utf-8");
					String pro=new String(request.getParameter("projector").getBytes("iso-8859-1"),"utf-8");
					String light=new String(request.getParameter("light").getBytes("iso-8859-1"),"utf-8");
					String wire=new String(request.getParameter("wireless").getBytes("iso-8859-1"),"utf-8");
					String lcd=new String(request.getParameter("lcd").getBytes("iso-8859-1"),"utf-8");
    				int pnumber=java.lang.Integer.valueOf(request.getParameter("person"));
    				if(java.lang.Integer.valueOf(request.getParameter("class"))==0){//搜尋所有類別的場地
    					sql="select * from PLACE WHERE PERSON>'"+pnumber+"' and AIR='"+pair+"' and DEAFENING='"+pdea+"' and ACOUSTICS='"+pacou+"' and MIR='"+mir+"' and SCREEN='"+scr+"' and PROJECTOR='"+pro+"' and LIGHT='"+light+"' and WIRELESS='"+wire+"' and LCD='"+lcd+"'";
    				}else{
    					sql="select * from PLACE WHERE P_CLASS='"+request.getParameter("class")+"' and PERSON>'"+pnumber+"' and AIR='"+pair+"' and DEAFENING='"+pdea+"' and ACOUSTICS='"+pacou+"' and MIR='"+mir+"' and SCREEN='"+scr+"' and PROJECTOR='"+pro+"' and LIGHT='"+light+"' and WIRELESS='"+wire+"' and LCD='"+lcd+"'";
    				}	
	  					rs=mydb.executeQuery(sql);
	  					ResultSet rs2;

    			%>
    			<tr>
   					<td colspan="3">
   					<%
   					if(rs.next()){%>
   					<%//秀借用時段的資料 %>
   						<table width="500" border="1" align="center">
  							<tr>
      							<td colspan="7" align="center" bgcolor="#FFFF66"><%=request.getParameter("b_date") %></td>
    						</tr>
  							<tr>
    							<td>場地編號</td>
    							<td>地點名稱</td>
    							<td>上午</td>
    							<td>中午</td>
    							<td>下午</td>
    							<td>晚上</td>
    							<td>預借</td>
  							</tr>
  							<%
  							do{//為使用者挑選出來的場地資料
  								char[]ch;
  								String strCh="";
  								int now_state=0;
  								int now_state_a=0;
  								int now_state_b=0;
  								int now_state_c=0;
  								int now_state_d=0;
  			 					%>
  								<form id="book" name="form1" method="post" action="add_Pbook.jsp">
  									<tr>
    									<td><a href="../PFManage/detailPlace.jsp?id=<%=rs.getString("P_ID")%>" target="_black" ><%=rs.getString("P_ID")%></a><input type="hidden" name="p_id" value="<%=rs.getString("P_ID")%>"></td>
    									<td><%=rs.getString("P_NAME")%><input type="hidden" name="p_name" value="<%=rs.getString("P_NAME")%>"></td>
  										<%//讀借用或預借的資料庫,看看此筆資料是不是已經被借用或預借,若有則不讓此場地設備再被預借!
  										
    									sql="select * from BOOK WHERE PF_ID='"+rs.getString("P_ID")+"' and APPLY_DATE='"+request.getParameter("b_date")+"'";
										rs2=mydb.executeQuery(sql);
										if(rs2.next()){//有預借的資料
											do{
												//判斷預借的時段,將之檔起來,不讓人預借
												now_state=1;
												ch = rs2.getString("BOOK_TIME").toCharArray();
												for(int i=0;i<ch.length;i++){
													strCh="";
													strCh+=ch[i];
													if(strCh.equalsIgnoreCase("a")){
														now_state_a=1;
													}else if(strCh.equalsIgnoreCase("b")){
														now_state_b=1;
													}else if(strCh.equalsIgnoreCase("c")){
														now_state_c=1;
													}else if(strCh.equalsIgnoreCase("d")){
														now_state_d=1;
													}
												}
											}while(rs2.next());
										}else{
											now_state=0;
										}
										//檢查借用資料
										sql="select * from BORROW WHERE PF_ID='"+rs.getString("P_ID")+"'and MUST_RETURN_DATE<'"+request.getParameter("b_date")+"'and RETURN_DATE is null";
										rs2=mydb.executeQuery(sql);
										if(rs2.next()){//假如此筆借用資料已經逾期,則不再讓人借用
											now_state_a=2;
											now_state_b=2;
											now_state_c=2;
											now_state_d=2;
										}else{
											sql="select * from BORROW WHERE PF_ID='"+rs.getString("P_ID")+"'and BORROW_DATE='"+request.getParameter("b_date")+"'";
											rs2=mydb.executeQuery(sql);
											if(rs2.next()){//有借用的資料
												now_state=2;
												ch = rs2.getString("BORROW_TIME").toCharArray();
												for(int i=0;i<ch.length;i++){
													strCh="";
													strCh+=ch[i];
													if(strCh.equalsIgnoreCase("a")){
														now_state_a=2;
													}else if(strCh.equalsIgnoreCase("b")){
														now_state_b=2;
													}else if(strCh.equalsIgnoreCase("c")){
														now_state_c=2;
													}else if(strCh.equalsIgnoreCase("d")){
														now_state_d=2;
												}
											}
											}else{
												now_state=0;
											}
										}
    									 %>
    									<td>
    										<%if(now_state_a==0) {
    											%>
    											<input type="checkbox" name="book_time" value="a" />
    											<%
    										}else if(now_state_a==1){%>
    											<img src="images/book.jpg"/><%
    										}else if(now_state_a==2){%>
    											<img src="images/borrow.jpg"/><%
    										}
    										%>
    									</td>
    									<td>
    										<%if(now_state_b==0) {
    											%>
    											<input type="checkbox" name="book_time" value="b" />
    											<%
    										}else if(now_state_b==1){%>
    											<img src="images/book.jpg"/><%
    										}else if(now_state_b==2){%>
    											<img src="images/borrow.jpg"/><%
    										}
    										%>
    									</td>
    									<td>
    										<%if(now_state_c==0) {
    											%>
    											<input type="checkbox" name="book_time" value="c" />
    											<%
    										}else if(now_state_c==1){%>
    											<img src="images/book.jpg"/><%
    										}else if(now_state_c==2){%>
    											<img src="images/borrow.jpg"/><%
    										}
    										%>
    									</td>
    									<td>
    										<%if(now_state_d==0) {
    										%>
    											<input type="checkbox" name="book_time" value="d" />
    											<%
    										}else if(now_state_d==1){%>
    											<img src="images/book.jpg"/><%
    										}else if(now_state_d==2){%>
    											<img src="images/borrow.jpg"/><%
    										}
    										%>
    									</td>
    					
    									<td>
    									<%
    										if(now_state_a!=0 && now_state_b!=0 && now_state_c!=0 && now_state_d!=0){
    											out.print("無法預借");
    										}else{%>
    											<input type="submit" name="Submit" value="預借" />
    											<%
    										}
    					 				%>
    									</td>
    									<input type="hidden" name="book_date" value="<%=request.getParameter("b_date")%>"/>
  									</tr>
  								</form>
  							<%		
  							}while(rs.next()); %>
						</table>
   					
   					<%//結束 %>
   					
   					<%	
   					}else{
	  						out.print("找不到符合的資料");
	  					}
   					 %>
   					</td>
  				</tr>
  				<%		
				} %>
			</table>
<%--內頁end--%>
    </td>
  </tr>
  <tr>
    <td colspan="2"></td>
  </tr>
</table>
<%
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>
</body>
</html>
